import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:nft_shop/login/controller/logic_controller.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/my_text_field.dart';

class LoginRegisterPage extends StatefulWidget {
  const LoginRegisterPage({Key? key}) : super(key: key);

  @override
  _LoginRegisterPageState createState() => _LoginRegisterPageState();
}

class _LoginRegisterPageState extends State<LoginRegisterPage> {
  //定义一个controller
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _vCodeController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _affirmPasswordController =
      TextEditingController();
  final TextEditingController _inviteController = TextEditingController();
  final TextEditingController _vImgCodeController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const MyAppBar(),
      body: MyScrollView(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 17.0),
        children: [
          Center(
              child: LoadAssetImage(
            "logo",
            width: 107,
            height: 104,
          )),
          SizedBox(height: 37),
          Text(
            "手机注册",
            style: TextStyle(
              fontSize: 26,
              color: Global.kTheme.textPrimary1,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          // SizedBox(height: 8),
          // Text(
          //   "您好，欢迎来到${Global.AppName}商城！",
          //   style: TextStyle(
          //     fontSize: 18,
          //     color: Global.kTheme.textPrimary10,
          //   ),
          // ),
          SizedBox(height: 29),
          Text(
            "手机号",
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary6,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          MyTextField(
            controller: _phoneController,
            keyboardType: TextInputType.phone,
            hintText: "请输入手机号",
          ),
          SizedBox(height: 10),
          Text(
            "验证码",
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary6,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          MyTextField(
            controller: _vCodeController,
            keyboardType: TextInputType.number,
            getVCode: () async {
              if (_phoneController.text.length == 0) {
                EasyLoading.showToast('请输入手机号');
                return false;
              }
              if (!RegexUtil.isMobileSimple(_phoneController.text)) {
                EasyLoading.showToast('手机号格式不正确');
                return false;
              }
              EasyLoading.show(status: "加载中...");
              String url =
                  await ServiceRepository.getImageCode(_phoneController.text);
              EasyLoading.dismiss();
              var imageCode = await showDialog(
                  context: context,
                  builder: (context) {
                    _vImgCodeController.text = "";
                    return Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.all(30),
                      child: SizedBox(
                        width: 300,
                        height: 330,
                        child: Container(
                          padding: EdgeInsets.all(30),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          child: Column(
                            children: [
                              Text(
                                "请输入验证码",
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Global.kTheme.textPrimary6,
                                  fontFamily: Global.mySemiboldFont,
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              LoadImage(
                                url,
                                fit: BoxFit.fitWidth,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              Container(
                                padding: EdgeInsets.only(left: 5, right: 5),
                                decoration: BoxDecoration(
                                  color: Color(0xffF4F4F4),
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(5)),
                                ),
                                child: MyTextField(
                                  keyboardType: TextInputType.phone,
                                  hintText: "请输入验证码",
                                  controller: _vImgCodeController,
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              MyButton(
                                  minHeight: 40,
                                  text: "确认",
                                  fontSize: 17,
                                  textColor: Global.kTheme.textPrimary5,
                                  backgroundColor: Global.kTheme.colorPrimary,
                                  onPressed: () {
                                    if (_vImgCodeController.text.length == 0) {
                                      EasyLoading.showToast('请输入验证码');
                                      return;
                                    }
                                    Navigator.of(context)
                                        .pop(_vImgCodeController.text);
                                  })
                            ],
                          ),
                        ),
                      ),
                    );
                  });
              if (ObjectUtil.isNotEmpty(imageCode)) {
                return LoginLogic.to.shayuSms(_phoneController.text, imageCode);
              } else {
                return false;
              }
            },
            maxLength: 6,
            hintText: "验证码",
          ),
          SizedBox(height: 10),
          Text(
            "密码",
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary6,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          MyTextField(
            keyName: 'password',
            isInputPwd: true,
            controller: _passwordController,
            keyboardType: TextInputType.visiblePassword,
            hintText: "含字母和数字且长度为6-20位",
          ),
          SizedBox(height: 10),
          Text(
            "再次输入密码",
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary6,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          MyTextField(
            keyName: 'password',
            isInputPwd: true,
            controller: _affirmPasswordController,
            keyboardType: TextInputType.visiblePassword,
            hintText: "再次输入密码",
          ),
          SizedBox(height: 10),
          Text(
            "邀请码",
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary6,
              fontFamily: Global.mySemiboldFont,
            ),
          ),
          MyTextField(
            keyName: 'invite',
            controller: _inviteController,
            hintText: "邀请码(非必填)",
          ),
          SizedBox(height: 40),
          Container(
            height: 40,
            decoration: BoxDecoration(
              color: Colors.white,
              // Colours.app_main,
              borderRadius: BorderRadius.circular(5),
            ),
            child: MyButton(
              key: const Key('backpasswork'),
              minHeight: 40,
              onPressed: () {
                if (_phoneController.text.length == 0) {
                  EasyLoading.showToast('请输入手机号');
                  return;
                }
                if (_vCodeController.text.length == 0) {
                  EasyLoading.showToast('请输入验证码');
                  return;
                }
                if (_passwordController.text.length == 0) {
                  EasyLoading.showToast('请输入密码');
                  return;
                }
                if (_affirmPasswordController.text.length == 0) {
                  EasyLoading.showToast('请输入确认密码');
                  return;
                }
                if (_passwordController.text !=
                    _affirmPasswordController.text) {
                  EasyLoading.showToast('两次密码不一致');
                  return;
                }

                LoginLogic.to.register(
                  username: _phoneController.text,
                  code: _vCodeController.text,
                  referee: _inviteController.text,
                  password: _passwordController.text,
                );
              },
              text: "注册",
              fontSize: 17,
              textColor: Global.kTheme.textPrimary5,
              backgroundColor: Global.kTheme.colorPrimary,
            ),
          ),
          SizedBox(height: 10),
        ],
      ),
    );
  }
}
